package com.dianning.service.scheduling;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.*;
import org.quartz.*;
import org.springframework.scheduling.quartz.*;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.dianning.bean.Channel;
import com.dianning.comm.Constant;
import com.dianning.service.ChannelService;
import com.dianning.service.InfoService;
import com.dianning.service.template.TemplateUtil;

import freemarker.template.Template;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class InfoNumStatJob extends QuartzJobBean {

	private static final Log logger = LogFactory.getLog(InfoNumStatJob.class);

	private FreeMarkerConfigurer freeMarkerConfigurer;
	private TemplateUtil templateUtil;

	public FreeMarkerConfigurer getFreeMarkerConfigurer() {
		return freeMarkerConfigurer;
	}

	public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {
		this.freeMarkerConfigurer = freeMarkerConfigurer;
	}

	public TemplateUtil getTemplateUtil() {
		return templateUtil;
	}

	public void setTemplateUtil(TemplateUtil templateUtil) {
		this.templateUtil = templateUtil;
	}

	public InfoNumStatJob() {
	}

	/**
	 * executeInternal
	 *
	 * @param jobExecutionContext
	 *            JobExecutionContext
	 * @throws JobExecutionException
	 * @todo Implement this org.springframework.scheduling.quartz.QuartzJobBean
	 *       method
	 */
	protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		try {
			Template t = this.getFreeMarkerConfigurer().getConfiguration().getTemplate("index.ftl");
			String input = t.toString();
			Map map=this.getTemplateUtil().getIndexPage(input);
			String html = FreeMarkerTemplateUtils.processTemplateIntoString(t, map);
			html = html.replaceAll("<!--\\{(.*?)\\}-->", "");
			File srcFile = new File(Constant.ROOTPATH + "index.jsp");
			FileUtils.writeStringToFile(srcFile, html, "UTF-8");
		} catch (Exception ex) {
			logger.info(ex);
		}
	}

}
